package com.manong.bookstore.store.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.manong.bookstore.store.feign.AdminFeignService;
import com.manong.bookstore.store.feign.MessageFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.manong.bookstore.store.entity.StoreEntity;
import com.manong.bookstore.store.service.StoreService;
import com.manong.common.utils.PageUtils;
import com.manong.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author cyq
 * @email 465447222@qq.com
 * @date 2021-01-27  21:38:42
 */
@RestController
@RequestMapping("store/store")
public class StoreController {
    @Autowired
    private StoreService storeService;

    @Autowired
    private AdminFeignService adminFeignService;

    @Autowired
    private MessageFeignService messageFeignService;

    /**
     * 列表
     */
//    @RequestMapping("/list")
//    public R list(@CookieValue("storeKeeperId") String skId ,@CookieValue("JSESSIONID") String sessionId, HttpServletRequest request, @RequestParam Map<String, Object> params){
//        if (sessionId!=null){
//            if(sessionId.equals(request.getSession().getId()) && skId.equals(request.getSession().getAttribute("storeKeeperId").toString())){
//                PageUtils page = storeService.queryPage(params);
//                return R.ok().put("page", page);
//            }
//        }
//        return R.error("请先登录!");
//    }
    /**
     * 用户留言初始化
     * 自动检查session判断当前用户是否已登录
     */
    @RequestMapping("/message/init/{storeId}")
    @ResponseBody
    public R initMessage(@CookieValue("storeKeeperId") String skId ,@CookieValue("JSESSIONID") String sessionId,HttpServletRequest request,@PathVariable("storeId") Integer storeId) {
        if (sessionId!=null){
            if(sessionId.equals(request.getSession().getId()) && skId.equals(request.getSession().getAttribute("storeKeeperId").toString())){
                System.out.println("initMessage: " + storeId);
                try {
                    Map<String, Object> params = new HashMap<>();
                    params.put("sendIdValue", storeId);
                    params.put("sendTypeValue", "store");
                    params.put("receiveIdValue", "");
                    params.put("receiveTypeValue", "");
                    params.put("messageIdValue", "");
                    params.put("contentValue", "");
                    R send = messageFeignService.LikeList(params);

                    params.put("sendIdValue", "");
                    params.put("sendTypeValue", "");
                    params.put("receiveIdValue", storeId);
                    params.put("receiveTypeValue", "store");
                    params.put("messageIdValue", "");
                    params.put("contentValue", "");
                    R receive = messageFeignService.LikeList(params);

                    return R.ok().put("status", "ok").put("send", send.get("page")).put("receive", receive.get("page"));
                }
                catch (Exception e) {
                    System.out.println(e.getMessage());
                    return R.ok().put("status", "error: message server error");
                }
            }
        }

        return R.ok().put("status", "error: no login");
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
            PageUtils page = storeService.queryPage(params);
            return R.ok().put("page", page);
    }

    /**
     * 信息
     */
//    @RequestMapping("/info/{storeId}")
//    public R info(@CookieValue("storeKeeperId") String skId ,@CookieValue("JSESSIONID") String sessionId, HttpServletRequest request,@PathVariable("storeId") Integer storeId){
//        if (sessionId!=null){
//            if(sessionId.equals(request.getSession().getId()) && skId.equals(request.getSession().getAttribute("storeKeeperId").toString())){
//                StoreEntity store = storeService.getById(storeId);
//                return R.ok().put("store", store);
//            }
//        }
//        return R.error("请先登录!");
//    }
    /**
     * 信息
     */
    @RequestMapping("/info/{storeId}")
    public R info(@PathVariable("storeId") Integer storeId){
            StoreEntity store = storeService.getById(storeId);
            String classifyName;
            Object[] classifies=null;
            try{
                classifyName=adminFeignService.getClassifyName(store.getClassifyId());
                store.setClassifyName(classifyName);
                classifies=adminFeignService.listClassifyNames(new HashMap<String, Object>());
            } catch (Exception e) {
                e.printStackTrace();
                return R.ok("分类获取失败").put("store", store);
            }
            return R.ok().put("store", store).put("classifies",classifies);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@CookieValue("storeKeeperId") String skId ,@CookieValue("JSESSIONID") String sessionId,HttpServletRequest request,@RequestBody StoreEntity store){
        if (sessionId!=null){
            if(sessionId.equals(request.getSession().getId()) && skId.equals(request.getSession().getAttribute("storeKeeperId").toString())){
                if(store.getStorekeeperId()==null||store.getStorekeeperId()==0)
                    return R.error("请先登录");
                storeService.save(store);
                return R.ok();
            }
        }
        return R.error("请先登录!");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@CookieValue("storeKeeperId") String skId,@CookieValue("JSESSIONID") String sessionId,HttpServletRequest request, @RequestBody StoreEntity store){
        if (sessionId!=null){
            if(sessionId.equals(request.getSession().getId()) && skId.equals(request.getSession().getAttribute("storeKeeperId").toString())){
                storeService.updateById(store);
                return R.ok();
            }
        }
        return R.error("请先登录!");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@CookieValue("storeKeeperId") String skId ,@CookieValue("JSESSIONID") String sessionId,HttpServletRequest request,@RequestBody Integer[] storeIds){
        if (sessionId!=null){
            if(sessionId.equals(request.getSession().getId()) && skId.equals(request.getSession().getAttribute("storeKeeperId").toString())){
                storeService.removeByIds(Arrays.asList(storeIds));
                return R.ok();
            }
        }
        return R.error("请先登录!");
    }

}
